<?php

namespace App\Admin\Repositories;

use App\Models\EacooCity;
use App\Models\EacooCountry;
use App\Models\EacooProv;
use App\Models\EacooTown;
use App\Models\EacooVillage;
use App\Models\User as Model;
use Dcat\Admin\Repositories\EloquentRepository;

class User extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;


    /**
     * 根据父级code获得地区列表
     * @param string $parentsCode 父级别code
     * @return array|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getAddressByPrentsCode($parentsCode = '')
    {
        switch (strlen($parentsCode)) {
            case 0:
                // 获得全部省
                $query = EacooProv::query();
                $query->select('code as id', 'prov as text');
                break;
            case 2:
                // 获得全部市
                $query = EacooCity::query();
                $query->select('code as id', 'city as text');
                $query->where('prov_code', $parentsCode);
                break;
            case 4:
                // 获得全部区
                $query = EacooCountry::query();
                $query->select('code as id', 'country as text');
                $query->where('city_code', $parentsCode);
                break;
            case 6:
                // 获得全部乡镇
                $query = EacooTown::query();
                $query->select('code as id', 'town as text');
                $query->where('country_code', $parentsCode);
                break;
            case 9:
            case 12:
                // 获得全部村委
                $query = EacooVillage::query();
                $query->select('code as id', 'village as text');
                $query->where('town_code', $parentsCode);
                break;
            default:
                return [];
        }

        $listData = $query->get();

        // 补全省份数据中的pid
        if (strlen($parentsCode) == 0) {
            foreach ($listData as &$listDataVal) {
                $listDataVal->pid = '';
            }
        }

        return $listData;
    }

    /**
     * 通过code获得地区信息
     * @param string $code
     * @return array|mixed|string
     */
    public static function getNameByCode($code = '')
    {
        switch (strlen($code)) {
            case 2:
                // 获得省
                $query = EacooProv::query();
                $query->select('prov as text');
                break;
            case 4:
                // 获得市
                $query = EacooCity::query();
                $query->select('city as text');
                break;
            case 6:
                // 获得区
                $query = EacooCountry::query();
                $query->select('country as text');
                break;
            case 9:
                // 获得镇
                // 最新的镇编号为12位，库中未更新的为9位，所以要兼容9位或者12位
            case 12:
                // 获得镇
                $query = EacooTown::query();
                $query->select('town as text');
//                // 获得村委
//                $query = EacooVillage::query();
//                $query->select('village as text');
                break;
            default:
                return [];
        }

        $query->where('code', $code);
        $data = $query->first();

        return $data ? $data->text : '';
    }

}
